package com.teatrace.service.pack.impl;

import java.util.List;

import com.teatrace.action.cfg.Basic;
import com.teatrace.commons.util.PageHelper;
import com.teatrace.dao.BaseDao;
import com.teatrace.dao.template.DaoCallBack;
import com.teatrace.dao.template.DaoTemplate;
import com.teatrace.dao.template.impl.AutoTransactionDaoTemplate;
import com.teatrace.model.pack.BoxProduct;
import com.teatrace.model.pack.GoodsType;
import com.teatrace.model.pack.PackingFlow;
import com.teatrace.model.pack.ParticleProduct;
import com.teatrace.model.process.SemiFinishedProductPackage;
import com.teatrace.service.pack.PackService;

public class PackServiceImpl implements PackService {
	
	private DaoTemplate getAutoTransactionDaoTemplate() {
		return new AutoTransactionDaoTemplate();
	}

	/**
	 * 查询信息
	 */
	@SuppressWarnings("unchecked")
	public <T> List<T> listByPage(final Class<T> clazz, final int page) throws Exception {
		// TODO Auto-generated method stub
		return (List<T>) this.getAutoTransactionDaoTemplate().execute(new DaoCallBack() {
			public Object doInDao(BaseDao dao) throws Exception {
				// TODO Auto-generated method stub
				return dao.listByPage(clazz, PageHelper.getFirstResult(page, Basic.pageSize), Basic.pageSize);
			}
		});
	}
	
	/**
	 * 修改信息
	 */
	public void update(final Object obj) throws Exception {
		this.getAutoTransactionDaoTemplate().execute(new DaoCallBack() {
			public Object doInDao(BaseDao dao) throws Exception {
				dao.update(obj);
				return null;
			}
		});
	}
	
	/**
	 * 增加信息
	 */
	public void add(final Object obj) throws Exception {
		this.getAutoTransactionDaoTemplate().execute(new DaoCallBack() {
			public Object doInDao(BaseDao dao) throws Exception {
				System.out.println("Object: " + obj);
				dao.save(obj);
				System.out.println("OK ADD");
				return null;
			}
		});
	}
	
	/**
	 * 删除信息
	 * 
	 */
	
	public void addPackingFlow(final PackingFlow packingFlow) throws Exception {
		// TODO Auto-generated method stub
		this.getAutoTransactionDaoTemplate().execute(new DaoCallBack() {
			public Object doInDao(BaseDao dao) throws Exception {
				// TODO Auto-generated method stub
				//List list = dao.list(goodsType.getClass());
				//dao.load(clazz, pk)
				//packingFlow.setGoodsType(1);
				dao.save(packingFlow);
				return null;
				
			}
		});
	}

	public void delPackingFlowById(final PackingFlow packingFlow) throws Exception {
		// TODO Auto-generated method stub
		this.getAutoTransactionDaoTemplate().execute(new DaoCallBack() {
			public Object doInDao(BaseDao dao) throws Exception {
				// TODO Auto-generated method stub
				dao.delete(packingFlow);
				return null;
			}
		});
	}

	public void updatePackingFlow(final PackingFlow packingFlow) throws Exception {
		// TODO Auto-generated method stub
		this.getAutoTransactionDaoTemplate().execute(new DaoCallBack() {
			public Object doInDao(BaseDao dao) throws Exception {
				// TODO Auto-generated method stub
				dao.update(packingFlow);
				return null;
			}
		});
	}

	public boolean findGoodsType(final GoodsType goodsType) throws Exception {
		// TODO Auto-generated method stub
		return (Boolean) this.getAutoTransactionDaoTemplate().execute(new DaoCallBack() {
			@SuppressWarnings("unchecked")
			public Object doInDao(BaseDao dao) throws Exception {
				// TODO Auto-generated method stub
				List list = dao.list(goodsType.getClass());
				if(list.size() == 0){
					return false;
				}
				return true;
			}
		});
	}

	@SuppressWarnings("unchecked")
	public List listBoxProduct(BoxProduct bp) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	public <T> List<T> listObject(final Class<T> clazz) throws Exception {
		// TODO Auto-generated method stub
		return (List) this.getAutoTransactionDaoTemplate().execute(new DaoCallBack() {
			public Object doInDao(BaseDao dao) throws Exception {
				// TODO Auto-generated method stub
				return dao.list(clazz);
			}
		});
	}
	
	@SuppressWarnings("unchecked")
	public List listParticleProduct(ParticleProduct pp) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	public List listSemifinishedProductPackage(SemiFinishedProductPackage spp)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
}
